<?php
/**
 * Klasse zur Berechnung des BodyMassIndex
 * 
 * @author		Joachim Ruf
 * @copyright	Copyright (c) 2003-2013 Loresoft Software (http://www.loresoft.de)
 * @license		http://yapary.loresoft.de/lizenzbedingungen     New BSD License
 * 
 * */


Class Yapary_Other_BodyMassIndex
{
	private $gender;
	private $bmiFrom;
	private $bmiTo;
	private $text;

	
    /**
     * Klasse zur Ueberprufung der Klassifikation fuer einen BodyMassIndex
     * @param string $gender	: Geschlecht {m|w}
     * @param string $bmiFrom	: Anfang des BMI-Wertes fur das entspr. Geschlecht
     * @param string $bmiTo		: Ende des BMI-Wertes fur das entspr. Geschlecht
     * @param string $text		: Definitionstext fuer den BMI
     * */	
	function __construct ($gender, $bmiFrom, $bmiTo, $text)
	{
		if ($gender == 'm' || $gender == 'w') {
			if (is_numeric($bmiFrom) && is_numeric($bmiTo)) {
				$this->gender = $gender;
				$this->bmiFrom = $bmiFrom;
				$this->bmiTo = $bmiTo;
				$this->text = $text;				
			}
		}
	}


	/**
	 * Prueft ob der BMI fuer das entspr. Geschlecht passt
     * @param string $gender	: Geschlecht {m|w}
     * @param string $bmi		: BMI-Werte
     * @return bool
	 * */
	public function isFit ($gender, $bmi)
	{
		if ($gender == $this->gender) {
			if ($bmi >= $this->bmiFrom && $bmi <= $this->bmiTo) {
				return true;
			}
		}
			
		return false;
	}

	
	/**
	 * Gibt den Definitionstext aus
	 * @return string
	 * */
	public function getText ( )	
	{
		return $this->text;
	}
}



Class Yapary_BMILimitState
{
	private $gender;
	private $bmiFrom;
	private $bmiTo;
	private $ageFrom;
	private $ageTo;
	
	
    /**
     * Klasse zur Ueberprufung des States fuer einen BodyMassIndex
     * @param string $gender	: Geschlecht {m|w}
     * @param string $bmiFrom	: Anfang des BMI-Wertes fur das entspr. Geschlecht
     * @param string $bmiTo		: Ende des BMI-Wertes fur das entspr. Geschlecht
     * @param string $text		: Definitionstext fuer den BMI
     * */	
	function __construct ( $gender, $ageFrom, $ageTo, $bmiFrom, $bmiTo )
	{
		if ($gender == 'm' || $gender == 'w') {
			if (is_numeric($ageFrom) && is_numeric($ageTo)) {
				if (is_numeric($bmiFrom) && is_numeric($bmiTo)) {
					$this->ageFrom = $ageFrom;
					$this->ageTo = $ageTo;
					$this->bmiFrom = $bmiFrom;
					$this->bmiTo = $bmiTo;
				}
			}
		}
	}

	
	/**
	 * Prueft ob der BMI fuer das entspr. Geschlecht passt
     * @param string $gender	: Geschlecht {m|w}
     * @param string $bmi		: BMI-Werte
     * @return bool
	 * */
	public function isFit ( $gender, $age )
	{
		//if( $gender == $this->gender )
			if ($age >= $this->ageFrom && $age <= $this->ageTo) {
				return true;
			}
			
		return false;
	}


	
	public function getBmiFrom ( )	
	{
		return $this->bmiFrom;
	}


	
	public function getBmiTo ( )
	{
		return $this->bmiTo;
	}
}



Class Yapary_BodyMassIndex
{
	private $gender;
	private $weight;
	private $size;
	private $age;
	private $arrClassification;	
	private $arrBmi;

	
	
	/**
	* Klasse zur Berechnung des BMI
	*/
	function __construct ($gender, $weight, $size, $age)
	{
		$this->gender = NULL;
		$this->weight = NULL;
		$this->size = NULL;
		$this->age = NULL;
		$this->arrClassification = array();
		
		/*
		Klassifikation	m		w
		Untergewicht	<20		<19
		Normalgewicht	20-25	19-24
		Übergewicht		25-30	24-30
		Adipositas		30-40	30-40
		massive Adipositas	>40	>40 
		*/
		$this->arrClassification[] = new Yapary_BMIClassificationState('m', 0, 19, 'Untergewicht');
		$this->arrClassification[] = new Yapary_BMIClassificationState('m', 20, 25, 'Normalgewicht');
		$this->arrClassification[] = new Yapary_BMIClassificationState('m', 26, 30, 'Übergewicht');
		$this->arrClassification[] = new Yapary_BMIClassificationState('m', 31, 40, 'Adipositas');
		$this->arrClassification[] = new Yapary_BMIClassificationState('m', 40, 100, 'massive Adipositas');

		$this->arrClassification[] = new Yapary_BMIClassificationState('w', 0, 18, 'Untergewicht');
		$this->arrClassification[] = new Yapary_BMIClassificationState('w', 19, 24, 'Normalgewicht');
		$this->arrClassification[] = new Yapary_BMIClassificationState('w', 25, 30, 'Übergewicht');
		$this->arrClassification[] = new Yapary_BMIClassificationState('w', 31, 40, 'Adipositas');
		$this->arrClassification[] = new Yapary_BMIClassificationState('w', 40, 100, 'massive Adipositas');

		/*
		BMI Age
		19-24 Jahre		19-24
		25-34 Jahre		20-25
		35-44 Jahre		21-26
		45-54 Jahre		22-27
		55-64 Jahre		23-28
		>64 Jahre		24-29
		*/
		// Geschlecht wird derzeit noch nicht unterschieden
		$this->arrBmi[] = new Yapary_BMILimitState('m', 19, 24, 19, 24);
		$this->arrBmi[] = new Yapary_BMILimitState('m', 25, 34, 20, 25);
		$this->arrBmi[] = new Yapary_BMILimitState('m', 35, 44, 21, 26);
		$this->arrBmi[] = new Yapary_BMILimitState('m', 45, 54, 22, 27);
		$this->arrBmi[] = new Yapary_BMILimitState('m', 55, 64, 23, 28);
		$this->arrBmi[] = new Yapary_BMILimitState('m', 64, 120, 24, 29);
		
		$this->setData ($gender, $weight, $size, $age);
	}	
		
	

	/**
	* Speichert das Geschlecht ab
	* @param char $gender : Geschlecht {m|w}
	* @return bool
	*/
	private function setGender ($gender)
	{
		if (!is_string($gender)) {
			return false;
		}
		if (strlen($gender) != 1) {
			return false;
		}
		if (!($gender == 'm' || $gender == 'w')) {
			return false;
		}
			
		$this->gender = $gender;
		return true;
	}

	
	
	/**
	* Speichert das Koerpergewicht ab
	* @param int $weight : Koerpergewicht {1-200}
	* @return bool
	*/
	private function setWeight ($weight)
	{
		if (!is_numeric($weight)) {
			return false;
		}
		if (!($weight >= 1 && $weight <= 200)) {
			return false;
		}
		
		$this->weight = $weight;
		return true;
	}

	
	
	/**
	* Speichert das Koerpergroesse ab
	* @param int $size : Koerpergroesse {20-250}
	* @return bool
	*/
	private function setSize ($size)
	{
		if (!is_numeric($size)) {
			return false;
		}
		if (!($size >= 20 && $size <= 250)) {
			return false;
		}
		
		$this->size = $size;
		return true;
	}

	
	
	/**
	* Speichert das Alter ab
	* @param int $age : Alter {19-120}
	* @return bool
	*/
	private function setAge ($age)
	{
		if (!is_numeric($age)) {
			return false;
		}
		if (!($age >= 19 && $age <= 120)) {
			return false;
	    }
		
		$this->age = $age;
		return true;
	}

	

	/**
	* Speichert die benoetigten Daten zur Berechnung ab
	* @param char $gender : Geschlecht {m|w}
	* @param int $weight : Koerpergewicht {1-200}
	* @param int $size : Koerpergroesse {20-250}
	* @param int $age : Alter {19-120}
	* @return bool
	*/
	private function setData ($gender, $weight, $size, $age)
	{
		if (!$this->setGender($gender)) {
		    return false;
		}
		if (!$this->setWeight($weight)) {
		    return false;
		}
		if (!$this->setSize($size)) {
		    return false;
		}
		if (!$this->setAge($age)) {
			return false;
		}

		return true;
	}



	/**
	* Liefert den idealen BodyMassIndex anhand der angegebenen Daten
	* @return string in Kg/m²|NULL
	*/
	public function getIdleBMI ()
	{
		if (count($this->arrBmi) == 0) {
		    return NULL;
		}
		
		while (list(, $state) = each($this->arrBmi)) {
			if ($state->isFit($this->gender, $this->age)) {
				return $state->getBmiFrom().'-'.$state->getBmiTo();
			}
		}
		
		return NULL;
	}



	/**
	* Liefert den realen BodyMassIndex anhand der angegebenen Daten
	* @return int in Kg/m²
	*/
	public function getRealBMI ()
	{
		$size = $this->size / 100;
		return round($this->weight / ($size * $size));
	}


	
	/**
	* Gibt den entsprechenden Text aus
	* @return string|NULL
	*/
	public function getByText ( )
	{			
		if (count($this->arrClassification) == 0) {
		    return NULL;
		}

		$size = $this->size / 100;
		$currentBmi = round($this->weight / ($size * $size));

		while (list(, $state) = each($this->arrClassification)) {
			if ($state->isFit($this->gender, $currentBmi)) {
				return $state->getText();
			}
		}
		
		return NULL;
	}	
	
	
	
}



?>